<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <!-- 内容 -->
      <el-card class="tree-card">
        <tree-tools :tree-node="company" is-root @addDepts="addDepts" />
        <div style="height: 20px" />
        <!-- 放置树形组件 -->
        <el-tree :data="departs" default-expand-all :props="defaultProps">
          <template #default="{ data }">
            <tree-tools :tree-node="data" @delDepts="getDepartments" @addDepts="addDepts" @editDepts="editDepts" />
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- 防止弹层 -->
    <add-dept
      ref="addDept"
      :show-dialog.sync="showDialog"
      :tree-node="node"
      @addDepts="getDepartments"
    />
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import AddDept from './components/add-dept.vue'
import { getDepartments } from '@/api/departments'
import { transListToTreeData } from '@/utils'
export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      company: { name: '', manager: '负责人', id: '' },
      departs: [],
      defaultProps: {
        label: 'name'
      },
      showDialog: false,
      node: null,
      loading: false
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      this.loading = true
      const { companyName, depts } = await getDepartments()
      this.company.name = companyName // 赋值公司数据
      this.departs = transListToTreeData(depts, '')
      this.loading = false
    },
    addDepts(node) {
      this.showDialog = true
      this.node = node
    },
    editDepts(node) {
      this.showDialog = true
      this.node = node
      this.$refs.addDept.getDepartDetail(node.id)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 20px 140px;
  font-size: 16px;
}

</style>
